package shootingtactics.client.game;

import shootingtactics.client.BaseComposite;
import shootingtactics.client.core.Constant;
import shootingtactics.client.gamebase.GameBase;

import com.google.gwt.canvas.client.Canvas;
import com.google.gwt.canvas.dom.client.Context2d;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;

public class GameTop extends BaseComposite{

    private static GameTopUiBinder uiBinder = GWT.create(GameTopUiBinder.class);

    interface GameTopUiBinder extends UiBinder<Widget, GameTop>{
    }

    GameBase gameBase;

    @UiField
    SimplePanel canvasPanel;

    @UiField
    Button startButton;

    @UiField
    Button stopButton;

    @UiField
    Button resetButton;

    public GameTop(){
        initWidget(uiBinder.createAndBindUi(this));
        setTop();
        setButton();
    }

    private void setButton(){
        startButton.addClickHandler(new ClickHandler(){

            public void onClick(ClickEvent event){
                setStartCanvas();

            }
        });

        stopButton.addClickHandler(new ClickHandler(){

            public void onClick(ClickEvent event){
                setStopCanvas();
            }
        });

        resetButton.addClickHandler(new ClickHandler(){

            public void onClick(ClickEvent event){
                setResetCanvas();

            }
        });
    }

    protected void setStartCanvas(){
        stopCanvas();
        startCanvas();
    }

    protected void setStopCanvas(){
        stopCanvas();
    }

    protected void setResetCanvas(){
        resetCanvas();
    }

    private void startCanvas(){
        gameBase.startTimer();
    }

    private void stopCanvas(){
        gameBase.stopTimer();
    }

    private void resetCanvas(){
        gameBase.resetTimer();
    }

    private void setTop(){
        Canvas canvas = Canvas.createIfSupported();
        canvas.setWidth(Constant.CANVAS_WIDTH + "px");
        canvas.setHeight(Constant.CANVAS_HEIGHT + "px");
        canvas.setCoordinateSpaceWidth(Constant.CANVAS_WIDTH);
        canvas.setCoordinateSpaceHeight(Constant.CANVAS_HEIGHT);

        Context2d backgroundContext = canvas.getContext2d();
        Context2d bulletContext = canvas.getContext2d();
        Context2d enemyContext = canvas.getContext2d();

        canvasPanel.add(canvas);

        gameBase = new GameBase(backgroundContext, bulletContext, enemyContext);
    }

}
